# -*- coding: utf-8 -*-

#    Copyright (C) 2014 by Promotux
#                       di Francesco Meloni snc - http://www.promotux.it/

#    Author: Francesco Meloni  <francesco@promotux.it>

#    This file is part of OdCollect.

#    OdCollect is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.

#    OdCollect is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with OdCollect.  If not, see <http://www.gnu.org/licenses/>.

from sqlalchemy import Table
from sqlalchemy.orm import mapper, relation
from Environment import *
import Environment
from lib.alembic.migration import MigrationContext
from lib.alembic.operations import Operations
from lib.alembic import op
from web_lib.webutils import leggiInSessione
from dao.Dao import Dao
from dao.DaoUtils import get_columns

schemaa = leggiInSessione(sezione="Main", chiave="schema")
try:
    t_categoria = Table('categoria', meta, schema=schemaa, autoload=True)
except:
    from data.tipoCategoria import t_tipo_categoria
    from data.categoria import t_categoria

colonne_categoria = get_columns(t_categoria)
if "descrizione" not in colonne_categoria:
    conn = engine.connect()
    ctx = MigrationContext.configure(conn)
    op = Operations(ctx)
    op.add_column('categoria', Column('descrizione', Text),schema=leggiInSessione(sezione="Main", chiave="schema"))
if "ordine" not in colonne_categoria:
    conn = engine.connect()
    ctx = MigrationContext.configure(conn)
    op = Operations(ctx)
    op.add_column('categoria', Column('ordine', Integer),schema=leggiInSessione(sezione="Main", chiave="schema"))

if "icona_font" not in colonne_categoria:
    conn = engine.connect()
    ctx = MigrationContext.configure(conn)
    op = Operations(ctx)
    op.add_column('categoria', Column('icona_font', String(20)),schema=leggiInSessione(sezione="Main", chiave="schema"))

if "colore_icona_font" not in colonne_categoria:
    conn = engine.connect()
    ctx = MigrationContext.configure(conn)
    op = Operations(ctx)
    op.add_column('categoria', Column('colore_icona_font', String(20)),schema=leggiInSessione(sezione="Main", chiave="schema"))


if "id_licenza" not in colonne_categoria:
    #try:
    conn = engine.connect()
    ctx = MigrationContext.configure(conn)
    op = Operations(ctx)
    op.add_column('categoria', Column('id_licenza',
            Integer,
                ForeignKey(fk_prefix_main + "licenza.id",
                onupdate="CASCADE",
                ondelete="RESTRICT"),
            nullable=True),schema=leggiInSessione(sezione="Main", chiave="schema"))
    #except:
        #delete_pickle()


from dao.TipoCategoria import TipoCategoria
from dao.TipoGeo import TipoGeo

class Categoria(Dao):

    def __init__(self, req=None):
        Dao.__init__(self, entity=self)

    def filter_values(self, k, v):
        if k == 'denominazione':
            dic = {k: t_categoria.c.denominazione.ilike("%" + v + "%")}
        return  dic[k]

    @property
    def tipo_categoria(self):
        if self.id_tipo_categoria:
            a = TipoCategoria().getRecord(id=self.id_tipo_categoria)
            if a:
                return a.denominazione
            else:
                return None
    @property
    def catalogo(self):
        return self.tipo_categoria

    @property
    def tipo_geo(self):
        if self.id_tipo_geo:
            a = TipoGeo().getRecord(id=self.id_tipo_geo)
            if a:
                return a.denominazione
            else:
                return None

std_mapper = mapper(Categoria, t_categoria, properties={
    'tipo_cate': relation(TipoCategoria, backref='categoria'),
    #'tipo_geoo': relation(TipoGeo,backref='categoria')
}, order_by=t_categoria.c.ordine)
